//
//  ViewController.m
//  QQ
//
//  Created by MAC on 16-3-10.
//  Copyright (c) 2016年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"
#import "CZMessage.h"
#import "CZMessageFrame.h"
#import "CZMessageCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *textinput;

//保存所有的消息的frame模型对象
@property(nonatomic,strong)NSMutableArray * messageFrames;

@end

@implementation ViewController

//懒加载数据

-(NSMutableArray*)messageFrames
{
    if(_messageFrames == nil)
    {
        NSString * path = [[NSBundle mainBundle]pathForResource:@"messages.plist" ofType:nil];
        NSArray*arrayDict = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray * arrayModels = [NSMutableArray array];
        
        for (NSDictionary* dict in arrayDict) {
            //创建一个数据模型
            CZMessage * model = [CZMessage messageWithDict:dict];
            //创建一个frame模型
            CZMessageFrame * modelFrame = [[CZMessageFrame alloc]init];
            modelFrame.message = model;
            //把frame模型加入到数组
            [arrayModels addObject:modelFrame];
        }
        _messageFrames = arrayModels;
    }
    return _messageFrames;
}


//数据原方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger i = self.messageFrames.count;
    return i;
    //return 9;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.获取模型数据
    CZMessageFrame * modelFrame = self.messageFrames[indexPath.row];
    //2.创建单元格,可以封装到类方法中
    static NSString *ID = @"message_cell";
    //UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ID];
    CZMessageCell*cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        //cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell = [[CZMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //3.把模型设置给单元格对象
    cell.messageFrame = modelFrame;
  
    //UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    //cell.textLabel.text = @"hello";
    //4.返回单元格
    return cell;
}


//返回行高方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CZMessageFrame *messageFrame = self.messageFrames[indexPath.row];
    return messageFrame.rowHeight;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
	// Do any additional setup after loading the view, typically from a nib.
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0  blue:236/255.0  alpha:1.0];
    
    //设置文本框最左侧由一段间距
    UIView * leftVw = [[UIView alloc]init];
    leftVw.frame = CGRectMake(0, 0, 5, 1);
    self.textinput.leftView = leftVw;
    self.textinput.leftViewMode = UITextFieldViewModeAlways;
    
    //处理键盘弹出事件，不需要管通知的发布者
    //通知的监听
    //1.创建一个NSNotificationCenter对象来监听
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //2.监听消息弹出的事件
    [center addObserver:self selector:@selector(keyboardUp:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

//键盘弹出通知事件的执行
-(void)keyboardUp:(NSNotification*)notoInfo
{
    //NSLog(@"键盘被弹起来了，注意");
    //NSLog(@"%@",userInfo);
    //获取键盘的y值
    CGRect rectEnd = [notoInfo.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat keyboardY = rectEnd.origin.y;
    //计算聊天记录和输入框的平移高度
    CGFloat upValue = keyboardY - self.view.frame.size.height;
    
    //实现平移
    self.view.transform = CGAffineTransformMakeTranslation(0, upValue);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
